package com.wen.controller;


import com.wen.bean.UserInfoBean;
import com.wen.model.UserModel;
import com.wen.uilts.MyUilts;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "UserInfoAddServlet", value = "/UserInfoAddServlet")
public class UserInfoAddServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //设置编码
//        request.setCharacterEncoding("utf-8"); filter代替
//        response.setCharacterEncoding("utf-8");
        //获取表单数据并把表单数据保存在实例中
        UserInfoBean userInfoBean = MyUilts.getNewInstance(UserInfoBean.class);
        // 如果有id主键字段，则获取该字段的值，
        //利用隐藏表单域
        if (StringUtils.isNotEmpty(request.getParameter("userId"))){
            userInfoBean.setUserId(Integer.valueOf(request.getParameter("userId")));
        }
        userInfoBean.setUserName(request.getParameter("userName"));
        userInfoBean.setUserPsw(request.getParameter("userPsw"));
        userInfoBean.setPhone(request.getParameter("phone"));
        System.out.println(request.getParameter("userDate"));
        userInfoBean.setUserDate(MyUilts.strConvertDate(request.getParameter("userDate"),"yyyy-MM-dd HH:mm:ss"));
        //调用model层进行数据保存的业务逻辑处理

        int i = MyUilts.getNewInstance(UserModel.class).addUserInfo(userInfoBean);
        //数据保存成功之后，跳转的保存结果界面
        if (i > 0){
            List<UserInfoBean> lst = MyUilts.getNewInstance(UserModel.class).queryUserInfoAll();
            request.getSession().setAttribute("lstUser", lst);
            request.getRequestDispatcher("WEB-INF/viewPages/userInfoB/UserInfoAddDetail.jsp").forward(request, response);
        }else {
            request.getRequestDispatcher("WEB-INF/viewPages/userInfoB/UserInfoAdd.jsp").forward(request, response);
        }
        //request.setAttribute("intAdd", i);
    }
}
